<?php

!defined('IN_TIPASK') && exit('Access Denied');

class answermodel {

    var $db;
    var $base;

    function answermodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }

    /*根据aid获取一个答案的内容，暂时无用*/
    function get($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."answer WHERE id='$id'");
    }

    //获取得最佳答案
    function get_best($qid) {
        $bestanswer =  $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."answer WHERE qid=$qid AND adopttime != 0");
        $bestanswer['adopttime']=tdate($bestanswer['adopttime']);
        $bestanswer['time']=tdate($bestanswer['time']);
        $bestanswer['author_avartar'] = get_avatar_dir($bestanswer['authorid']);
        $bestanswer['tags'] = tstripslashes(unserialize($bestanswer['tag']));
        return $bestanswer;
    }

    /*根据qid获取答案的列表，用于在浏览一个问题的时候显示用*/
    function list_by_qid($qid) {
        $answerlist=array();
        $already=0;
        $query=$this->db->query("SELECT * FROM ".DB_TABLEPRE."answer WHERE qid=$qid ORDER BY time ASC");
        while($answer=$this->db->fetch_array($query)) {
            if($answer['authorid']==$this->base->user['uid']) {
                $already=1;
            }
            if(0==$answer['status']) {
                $answer['content']='本回答正在审核中...';
            }
            $answer['time']=tdate($answer['time']);
            $answer['author_avartar'] = get_avatar_dir($answer['authorid']);
            $answer['tags'] = tstripslashes(unserialize($answer['tag']));
            $answerlist[]= $answer;
        }
        return array($answerlist,$already);
    }

    /*根据uid获取答案的列表，用于在用户中心，我的回答显示*/
    function list_by_uid($uid , $start=0 , $limit=10) {
        $answerlist = array();
        $query = $this->db->query('SELECT * FROM `'.DB_TABLEPRE.'answer` WHERE `authorid`='.$uid.' ORDER BY `time` DESC LIMIT '.$start.','.$limit);
        while($answer=$this->db->fetch_array($query)) {
            $answer['time']=tdate($answer['time']);
            $answerlist[] = $answer;
        }
        return $answerlist;
    }

    /*添加答案*/
    function add($qid,$title,$content,$status=0) {
        $uid=$this->base->user['uid'];
        $username=$this->base->user['username'];
        $this->db->query( "INSERT INTO ".DB_TABLEPRE."answer SET qid='$qid',title='$title',author='$username',authorid='$uid',time='{$this->base->time}',content='$content',status=$status" );
        $this->db->query( "UPDATE ".DB_TABLEPRE."question SET  answers=answers+1  WHERE id=".$qid );
        $this->db->query("UPDATE ".DB_TABLEPRE."user SET answers=answers+1 WHERE  uid =$uid");
        $aid = $this->db->insert_id();
    }

    /*采纳指定的答案，问题状态变为2 已解决*/
    function adopt($qid,$answer,$comment='') {
        $time = $this->base->time;
        $this->db->query( "UPDATE ".DB_TABLEPRE."answer SET  adopttime='$time',comment='$comment' WHERE id=".$answer['id'] );
        $this->db->query( "UPDATE ".DB_TABLEPRE."question SET  status=2 ,`endtime`='$time' WHERE id=".$qid );
        $this->db->query("UPDATE ".DB_TABLEPRE."user SET adopts=adopts+1 WHERE  uid=".$answer['authorid']);
    }


    /*添加追问--追问--回答*/
    function add_tag($aid,$tagstr,$tagliststr){
    	$taglist = array();
        $tagliststr && $taglist = tstripslashes(unserialize($tagliststr));
        $taglist[] = $tagstr;
        $this->db->query("UPDATE ".DB_TABLEPRE."answer SET `tag`='".addslashes(serialize($taglist))."' WHERE `id`=$aid");
    }

    /*修改回答，同时重置回答的状态*/
    function update_content($aid,$content,$status=0) {
        $this->db->query("UPDATE `".DB_TABLEPRE."answer` set content='$content',status=$status  WHERE `id` =$aid");
    }


    /*投票结果*/
    function votelist($qid) {
        $votelist=array();
        $query=$this->db->query( "SELECT `author`,`votes` FROM ".DB_TABLEPRE."answer WHERE qid =$qid  AND voted =1 ORDER BY votes DESC");
        while($answer=$this->db->fetch_array($query)) {
            //$answer['time']=tdate($answer['time']);
            $votelist[] = $answer;
        }
        return $votelist;
    }

    /*后台回答搜索*/
    function list_by_search($title='',$author='',$keyword='',$datestart='',$dateend='',$start=0,$limit=10) {
        $sql="SELECT * FROM `".DB_TABLEPRE."answer` WHERE 1=1 ";
        $title && ($sql.=" AND `title` like '$title%' ");
        $author && ($sql.=" AND `author`='$author'");
        $keyword && ($sql.=" AND `content` like '$keyword%' ");
        $datestart && ($sql .= " AND `time`>= ".strtotime($datestart));
        $dateend && ($sql .=" AND `time`<= " .strtotime($dateend));
        $sql.=" ORDER BY `id` DESC LIMIT $start,$limit ";
        $answerlist = array();
        $query = $this->db->query($sql);
        while($answer=$this->db->fetch_array($query)) {
            $answer['time'] = tdate($answer['time']);
            $answerlist[] = $answer;
        }
        return $answerlist;
    }

    function rownum_by_search($title='',$author='',$keyword='',$datestart='',$dateend='') {
        $condition=" 1=1 ";
        $title && ($condition.=" AND `title` like '$title%' ");
        $author && ($condition.=" AND `author`='$author'");
        $keyword && ($condition.=" AND `content` like '$keyword%' ");
        $datestart && ($condition .= " AND `time`>= ".strtotime($datestart));
        $dateend && ($condition .=" AND `time`<= " .strtotime($dateend));
        return $this->db->fetch_total('answer',$condition);
    }

    function list_by_condition($condition , $start=0 , $limit=10) {
        $answerlist = array();
        $query = $this->db->query("SELECT * FROM `".DB_TABLEPRE."answer` WHERE $condition limit $start,$limit");
        while($answer=$this->db->fetch_array($query)) {
            $answer['time'] = tdate($answer['time']);
            $answerlist[] = $answer;
        }
        return $answerlist;
    }

    function remove($aids) {
        $this->db->query("DELETE FROM `".DB_TABLEPRE."answer` WHERE `id` IN ($aids)");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."vote` WHERE `aid` IN ($aids)");
    }

    function remove_by_qid($aid,$qid) {
        $this->db->query("DELETE FROM `".DB_TABLEPRE."answer` WHERE `id`=$aid");
        $this->db->query("DELETE FROM `".DB_TABLEPRE."vote` WHERE `aid`=$aid");
        $this->db->query("UPDATE `".DB_TABLEPRE."question` SET answers=answers-1 WHERE `id`=$qid");
    }

    function update_time_content($aid , $time , $content) {
        $this->db->query("UPDATE `".DB_TABLEPRE."answer` SET `content`='$content',`time`=$time WHERE `id`=$aid");
    }

    function change_to_verify($aids) {
        $this->db->query("UPDATE `".DB_TABLEPRE."answer` SET `status`=1 WHERE `status`=0 AND `id` IN ($aids)");
    }
}
?>
